package com.wfk.service.impl;

import com.wfk.entity.Customer;
import com.wfk.mapper.CustomerMapper;
import com.wfk.service.IAccountService;
import com.wfk.service.ICustomerService;
import com.wfk.utils.BasePage;
import com.wfk.utils.ValidateUtil;
import com.wfk.vo.MemberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员信息表 服务实现类
 * </p>
 *
 * @since 2019-01-07
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
	@Autowired
	private IAccountService accountService;
	@Autowired
	private CustomerMapper customerMapper;
	@Override
	public BasePage<Customer> listPage(BasePage<Customer> page, Customer cus) {
		QueryWrapper<Customer> queryWrapper = new QueryWrapper<Customer>();
		if(cus.getId()!=null) {
			queryWrapper.eq("id",cus.getId());
		}
		if(ValidateUtil.empty(cus.getNikname())) {
			queryWrapper.like("nikname",cus.getNikname());
		}
		if(ValidateUtil.empty(cus.getPhone())) {
			queryWrapper.like("phone",cus.getPhone());
		}
		if(!ValidateUtil.objEmpty(cus.getLevelid())) {
			queryWrapper.eq("levelid",cus.getLevelid());
		}
		if(cus.getParentid()!=null) {
			queryWrapper.eq("parentid", cus.getParentid());
		}
		if(ValidateUtil.date(cus.getStart())) {
			queryWrapper.ge("createtime", cus.getStart());
		}
		if(ValidateUtil.date(cus.getEnd())) {
			queryWrapper.le("createtime", cus.getEnd());
		}
		queryWrapper.orderByDesc("id");
		if(page.isPage()) {
			super.page(page, queryWrapper);
		}else {
			List<Customer> list = super.list(queryWrapper);
			page.setRecords(list);
		}
		for(Customer c:page.getRecords()) {
			c.setAccount(accountService.get(c.getId()));
		}
		return page;
	}

	@Override
	public BasePage<MemberVo> myTeam(BasePage<MemberVo> page, MemberVo vo) {
		if(vo.getCid()==null) {
			return page;
		}else if(vo.getCid()==null) {
			return page;
		}
		Customer cus = super.getById(vo.getCid());
		if(cus==null) {
			return page;
		}
		vo.setGeneration(cus.getGeneration());
		if(page.isPage()) {
			this.customerMapper.myTeam(page, vo);
		}
		return page;
	}

}
